package com.augrain.microframework.trace.common;

import com.augrain.microframework.trace.context.TraceContextSnapshot;
import lombok.Getter;
import lombok.Setter;
import lombok.ToString;

/**
 * trace标签
 *
 * @author biaoy
 * @since 2021/08/20
 */
@Getter
@Setter
@ToString
public class TraceLabel {

    /**
     * 上一个应用名称
     */
    private String fromApp;

    /**
     * 上一个应用ip
     */
    private String fromIp;

    /**
     * 当前追踪的traceId
     */
    private String traceId;

    /**
     * 当前追踪的spanId
     */
    private String spanId;

    private int spanIdIndex = 1;

    public TraceLabel() {
    }

    public TraceLabel(String fromApp, String fromIp, String traceId, String spanId) {
        this.fromApp = fromApp;
        this.fromIp = fromIp;
        this.traceId = traceId;
        this.spanId = spanId;
    }

    public static TraceLabel from(TraceContextSnapshot snapshot) {
        TraceLabel traceLabel = new TraceLabel();
        traceLabel.setTraceId(snapshot.getTraceId());
        traceLabel.setSpanId(snapshot.getSpanId());
        traceLabel.setFromIp(snapshot.getFromIp());
        traceLabel.setFromApp(snapshot.getFromApp());
        return traceLabel;
    }
}
